<?php
namespace App\Dao;

use App\Api\DealDay;
class DayCountTable extends \Lavender\Dao\Table
{
    protected $database = "hangqing_count";
    protected $table = "day_count";
    
    public static function get_last_day_data()
    {
        $date = DealDay::get_last_day();
        if(!$date || empty($date))
        {
            return null;
        }
        $datas = self::instance()->get(null,
            [],
            ['date'=>$date]
        );
        return $datas;
    }
    
    public static function get_last_5day_data()
    {
    	$days = DealDay::get_all_day();
    	$max_day = $days[0];
    	$min_day = $days[4];
    	$model = self::instance();
    	$sql = "SELECT `cid`,`code`,
    	        COUNT(*) as `day_num`,
    			AVG(`amount`) as `amount`,
    			SUM(`volume`) as `volume`,
    			SUM(`cost_money`) as `cost_money`,
    			MAX(`highest_price`) as `highest_price`,
    			MIN(`lowest_price`) as `lowest_price`,
    			AVG(`price_wave_lv`) as `price_wave_lv`,
    			ROUND(`cost_money`/`volume`,2) as `avg_price`
    			FROM `day_count`
    			WHERE `date` >= '{$min_day}' AND `date` <= '{$max_day}'
    			GROUP BY `cid`
    			";
    	return $model->get_by_sql($sql);
    	
    }
}

?>